package com.xyzyzglxt.web.controller.volunteer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.xyzyzglxt.common.core.domain.vo.EventCheckVo;
import com.xyzyzglxt.common.core.domain.vo.EventStateVo;
import com.xyzyzglxt.common.core.domain.vo.NumNowdayVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xyzyzglxt.common.annotation.Log;
import com.xyzyzglxt.common.core.controller.BaseController;
import com.xyzyzglxt.common.core.domain.AjaxResult;
import com.xyzyzglxt.common.enums.BusinessType;
import com.xyzyzglxt.system.domain.SysEvent;
import com.xyzyzglxt.system.service.ISysEventService;
import com.xyzyzglxt.common.utils.poi.ExcelUtil;
import com.xyzyzglxt.common.core.page.TableDataInfo;

/**
 * 活动信息Controller
 *
 * @author xyzyzglxt
 * @date 2022-01-17
 */
@Api(tags = "志愿活动信息管理")
@RestController
@RequestMapping("/volunteer/event")
public class SysEventController extends BaseController
{
    @Autowired
    private ISysEventService sysEventService;

    /**
     * 查询活动信息列表
     */
    @ApiOperation("查询活动信息列表")
    @PreAuthorize("@ss.hasPermi('volunteer:event:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysEvent sysEvent)
    {
        startPage();
        List<SysEvent> list = sysEventService.selectSysEventList(sysEvent);
        return getDataTable(list);
    }

    /**
     * 获取活动下拉树列表
     */
    @ApiOperation("获取活动下拉树列表")
    @GetMapping("/treeEventSelect")
    public AjaxResult treeselectEvent()
    {
        List<SysEvent> sysDepts = sysEventService.selectSysEventList(new SysEvent());
        return AjaxResult.success(sysEventService.buildEventTreeSelect(sysDepts));
    }

    /**
     * 导出活动信息列表
     */
    @ApiOperation("导出活动信息列表")
    @PreAuthorize("@ss.hasPermi('volunteer:event:export')")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEvent sysEvent)
    {
        List<SysEvent> list = sysEventService.selectSysEventList(sysEvent);
        ExcelUtil<SysEvent> util = new ExcelUtil<SysEvent>(SysEvent.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 活动发布
     * 新增活动信息
     */
    @ApiOperation("新增活动信息(招募单位新增活动，审核状态强制为未审核)")
    @PreAuthorize("@ss.hasPermi('volunteer:event:add')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysEvent sysEvent)
    {
        return toAjax(sysEventService.insertSysEvent(sysEvent));
    }


    /**
     * 修改活动信息
     */
    @ApiOperation("修改活动信息（给招募单位拟接口,不含修改活动审核）")
    @PreAuthorize("@ss.hasPermi('volunteer:event:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysEvent sysEvent)
    {
        int num = sysEventService.updateSysEvent(sysEvent);
        if(num == -10){
            return error("只能操作自己的发布的活动");
        }else if(num == -1){
            return error("活动已审核不允许修改");
        }
        return success();
    }

    /**
     * 审核不通过
     */
    @ApiOperation("修改活动信息（给管理者的接口，用于审核不通过）")
    @PreAuthorize("@ss.hasPermi('volunteer:event:check')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping("/checkFail")
    public AjaxResult checkFail(Long eventId)
    {
        SysEvent sysEvent = new SysEvent();
        sysEvent.setEventId(eventId);
        int num = sysEventService.FailSysEvent(sysEvent);
        if(num == -12){
            return error("仅管理员操作");
        }else if(num == -13){
            return error("审核时间需在活动开始之前");
        }
        return success();
    }

    /**
     * 审核通过
     */
    @ApiOperation("修改活动信息（给管理者的接口，用于审核通过）")
    @PreAuthorize("@ss.hasPermi('volunteer:event:check')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping("/checkSuccess")
    public AjaxResult checkSuccess(Long eventId)
    {
        SysEvent sysEvent = new SysEvent();
        sysEvent.setEventId(eventId);
        int num = sysEventService.SuccessSysEvent(sysEvent);
        if(num == -12){
            return error("仅管理员操作");
        }else if(num == -13){
            return error("审核时间需在活动开始之前");
        }else if(num == -14){
            return error("活动已审核！");
        }
        return success();
    }

    @ApiOperation("删除活动信息")
    @PreAuthorize("@ss.hasPermi('volunteer:event:singleDel')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/singleDel/{eventIds}")
    public AjaxResult singleDel(@PathVariable Long eventIds){
        int num = sysEventService.deleteSysEventByEventId(eventIds);
        if(num == -10){
            return error("只能自己的发布的活动");
        }else if(num == -1){
            return error("活动已开始不允许删除");
        }
        return success();
    }

    /**
     * 批量删除活动信息
     */
    @ApiOperation("批量删除活动信息")
    @PreAuthorize("@ss.hasPermi('volunteer:event:remove')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eventIds}")
    public AjaxResult remove(@PathVariable Long[] eventIds)
    {
        int num = sysEventService.deleteSysEventByEventIds(eventIds);
        if(num == -10){
            return error("只能自己的发布的活动");
        }else if(num == -1){
            return error("活动已开始不允许删除");
        }
        return success();
    }

    /**
     * 获取活动信息详细信息 (OK)
     */
    @ApiOperation("根据id查询活动信息详情")
    @PreAuthorize("@ss.hasPermi('volunteer:event:query')")
    @GetMapping(value = "/{eventId}")
    public AjaxResult getInfo(@PathVariable("eventId") Long eventId)
    {
        return AjaxResult.success(sysEventService.selectSysEventByEventId(eventId));
    }

    /**
     * 返回活动状态及名称
     * @return
     */
    @ApiOperation("返回活动状态及名称")
    @GetMapping("/eventState")
    public TableDataInfo eventState(){
        startPage();
        List<EventStateVo> eventStateVos = sysEventService.eventState();
        return getDataTable(eventStateVos);
    }

    /**
     * 返回活动审核名称
     * @return
     */
    @ApiOperation("返回活动审核名称")
    @GetMapping("/eventCheckInfo")
    public TableDataInfo eventCheckInfo(){
        List<EventCheckVo> eventCheckInfos = sysEventService.eventCheckInfo();
        return getDataTable(eventCheckInfos);
    }

    /**
     * 当天时间活动数and报名人数
     * @return
     */
    @ApiOperation("当天时间活动数and报名人数")
    @GetMapping("/findNumByNowday")
    public TableDataInfo findNumByNowday(){
        List<NumNowdayVo> numNowdayVo = sysEventService.findNumByNowday();
        return getDataTable(numNowdayVo);
    }

}
